/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.star.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.star.modules.sys.enums.IfEnum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.star.modules.sys.entity.ShMessage;
import com.star.modules.sys.service.ShMessageService;

/**
 * 在线留言Controller
 * @author L
 * @version 2018-09-03
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/shMessage")
public class ShMessageController extends BaseController {

	@Autowired
	private ShMessageService shMessageService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ShMessage get(String id, boolean isNewRecord) {
		return shMessageService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("sys:shMessage:view")
	@RequestMapping(value = {"list", ""})
	public String list(ShMessage shMessage, Model model) {
		model.addAttribute("shMessage", shMessage);
		return "modules/sys/shMessageList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sys:shMessage:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ShMessage> listData(ShMessage shMessage, HttpServletRequest request, HttpServletResponse response) {
		Page<ShMessage> page = shMessageService.findPage(new Page<ShMessage>(request, response), shMessage); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sys:shMessage:view")
	@RequestMapping(value = "form")
	public String form(ShMessage shMessage, Model model) {
		if(shMessage.getAsNews().equals(IfEnum.FALSE.getCode())){
			shMessage.setAsNews(IfEnum.TRUN.getCode());
			shMessageService.update(shMessage);
		}
		model.addAttribute("shMessage", shMessage);
		return "modules/sys/shMessageForm";
	}

	/**
	 * 保存在线留言
	 */
	@RequiresPermissions("sys:shMessage:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ShMessage shMessage) {
		shMessageService.save(shMessage);
		return renderResult(Global.TRUE, "保存在线留言成功！");
	}
	
	/**
	 * 删除在线留言
	 */
	@RequiresPermissions("sys:shMessage:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ShMessage shMessage) {
		shMessageService.delete(shMessage);
		return renderResult(Global.TRUE, "删除在线留言成功！");
	}
	
}